<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    body {
      height: 3000px;
      background: linear-gradient(pink, skyblue, blue);
    }

    #btn {
      position: fixed;
      bottom: 50px;
      right: 20px;
      background: darkblue;
      color: white;
      width: 120px;
      height: 50px;
      line-height: 50px;
      text-align: center;
      display: none;
    }
  </style>
</head>

<body>
  <div id="btn">回到顶部</div>
  <script src="./jquery-3.4.1.min.js"></script>
  <script>
    $(window).scroll(function () {
      // 获取上滚动距离
      var top = $(window).scrollTop()
      if (top > 400) {
        $("#btn").show();
      } else {
        $("#btn").hide();
      }
    })
    // 点击按钮运动回顶部
    $("#btn").click(function () {
      $("body,html").animate({ scrollTop: 0 }, 1000)
    })


  </script>
</body>

</html>